package com.bbx.taxi.client.DB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bbx.api.sdk.model.comm.returns.line.Cities;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.taxi.client.Bean.Line;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean;

/**
 * 线路
 * 
 */

public class LineDB implements DBcolumns {
	private DBHelper helper;

	public LineDB() {
		helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
	}

	/**
	 * 打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() {
		if(helper==null){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
		}
		SQLiteDatabase SQLdb ;
		try{SQLdb= helper.getReadableDatabase();}catch(Exception e){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
			SQLdb= helper.getReadableDatabase();
		}
		return SQLdb;
	}

	/**
	 * 打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() {
		if(helper==null){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
		}
		SQLiteDatabase SQLdb ;
		try{SQLdb= helper.getReadableDatabase();}catch(Exception e){
			helper = DBHelper.getInstance(MyApplication.getInstance().getApplicationContext(), null);
			SQLdb= helper.getReadableDatabase();
		}
		return SQLdb;
	}

	/**
	 * 查询
	 */
	public Cursor onQuery(String incity) {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_LINE, null, 
				LINE_INCITY + "='"+ incity+"'" 
				, null, null, null, LINE_ID + " ");
		return cursor;

	}
	/**
	 * 查询
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_LINE, null, 
				null
				, null, null, null, LINE_ID + " ");
		return cursor;

	}
	/**
	 * 根据城市的cn_name[中文]
	 */
	public Cursor onQuery(String cn_name,String incity) {
		SQLiteDatabase SQLdb = onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_LINE, null,
				LINE_NAME + "='"+ cn_name + "' and " +
				LINE_INCITY + "='"+ incity+"'" 
				, null, null, null, LINE_ID + " ");
		return cursor;

	}
	/**
	 * 读取出发地父类
	 */
	public ArrayList<AppCitysBean> readStartDB(boolean inCity) {
		ArrayList<AppCitysBean> list = new ArrayList<AppCitysBean>();
		AppCitysBean mAppCitysBean = null;
		Cursor c = onQuery(inCity?Value.CITY_INCITY:Value.CITY_NOINCITY);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				mAppCitysBean = (AppCitysBean) new JsonBuild().getData(AppCitysBean.class,
						map.get(DBcolumns.LINE_JSON));
				list.add(mAppCitysBean);
			} while (c.moveToNext());
		}
		onColse();
		return list;
	}

	/**
	 * 删除
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.delete(TABLE_LINE, null, null);
		SQLdb.close();
	}

	/**
	 * 删除
	 */
	public void onDelete(String name) {
		SQLiteDatabase SQLdb = onOpenWrite();
		String[] whereArgs = { String.valueOf(name) };
		int i = SQLdb.delete(TABLE_LINE, LINE_NAME + "=?", whereArgs);
		SQLdb.close();
	}

	/**
	 * 插入一条数据
	 */
	public void onWrite(String city,String name,String province,String incity,String json) {
		// 写入之前，先清空同一个城市
		// onDelete(cn_name);
		SQLiteDatabase SQLdb = onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(LINE_NAME, name);
		values.put(LINE_CITY, city);
		values.put(LINE_PROVINCE, province);
		values.put(LINE_INCITY, incity);
		values.put(LINE_JSON, json);
		SQLdb.insert(TABLE_LINE, LINE_ID, values);
		SQLdb.close();
	}

	/**
	 * 关闭数据库
	 */
	public void onColse() {
		SQLiteDatabase SQLdb = onOpenWrite();
		SQLdb.close();
	}
}
